class Point {
    constructor(x, y) {
        this.x = x;
        this.y = y;
    }
}

class Polygon extends Shape {
    /**
     * 正多边形
     * @param {*} centerX 
     * @param {*} centerY 
     * @param {*} radius 
     * @param {*} sides 边数
     * @param {*} startAngle 
     * @param {*} strokeStyle 
     * @param {*} fillStyle 
     */
    constructor(centerX, centerY, radius, sides, startAngle = 0) {
        super();
        this.x = centerX;
        this.y = centerY;
        this.radius = radius;
        this.sides = sides;
        this.startAngle = startAngle;
    }
    getPoints() {
        let points = [];
        let angle = this.startAngle;
        let offset = Math.PI * 2 / this.sides;
        for (let i = 0; i < this.sides; i++) {
            let x = this.x + this.radius * Math.cos(angle);
            let y = this.y + this.radius * Math.sin(angle);

            let p = new Point(x, y);

            angle += offset;
            points.push(p);
        }

        return points;

    }
    createPath(ctx) {
        let points = this.getPoints();
        ctx.beginPath();
        ctx.moveTo(points[0].x, points[0].y);

        for (let i = 1; i < this.sides; i++) {
            ctx.lineTo(points[i].x, points[i].y);
        }
        ctx.closePath();
    }

    draw(ctx) {
        super.begin(ctx);
        this.createPath(ctx);
        super.end(ctx);
    }

    containsPoint(ctx, x, y) {
        this.createPath(ctx);
        return ctx.isPointInPath(x, y);
    }
    move(x, y) {
        this.x = x;
        this.y = y;
    }
}